// pages/home/home.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    category:null,
    partnerLst:[],
    telPhone:null,
    address:null,
    hotarticle:[],
    successNum:0,
    allLoaded:false
    
  },
  //拨打电话
  phoneCall(){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.telPhone.value,
      success: function(res) {

      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  getBanner(){
    var key = app.getKey();
    var that = this;
    app.ajax('/Home/Index/carousel', "POST", { key }, function (res) {
      that.setData({
        imgUrls: res.data.data,
        successNum:that.data.successNum+1
      })
      that.isLoaded()
    })
  },
   //获取合作伙伴
  getPartnerLst(){
    console.log(app)
    var key = app.getKey();
    var that = this;
    app.ajax('/Home/Index/partnerLst',"POST",{key},function(res){
      that.setData({
        partnerLst:res.data.data,
        successNum: that.data.successNum + 1
      })
      that.isLoaded()
    })
  },
  //获取电话
  getTelPhone(){
    var key = app.getKey();
    var that = this;
    app.ajax('/Home/Index/getTelPhone', "POST", { key }, function (res) {
      that.setData({
        telPhone: res.data.data,
        successNum: that.data.successNum + 1
      })
      that.isLoaded()
    })
  },
  //获取地址
  getAddress(){
    var key = app.getKey();
    var that = this;
    app.ajax('/Home/Index/companyAddress', "POST", { key }, function (res) {
      that.setData({
        address: res.data.data,
        successNum: that.data.successNum + 1
      })
      that.isLoaded()
    })
  },
  //获取分类
  getCategory(){
    var key = app.getKey();
    var that = this;
    app.ajax('/Home/Category/index', "POST", { key:key }, function (res) {
      that.setData({
        category: res.data.data,
        successNum: that.data.successNum + 1
      })
      that.isLoaded()
    })
  },
  //跳转到分类
  toClassify(){
    app.globalData.cateId = '';
    wx.switchTab({
      url: '../classify/classify'
    })
  },
  toCate(event){
    let id = event.currentTarget.dataset.id;
    app.globalData.cateId = id;
    wx.switchTab({
      url: '../classify/classify'
    })
  },
  //获取热门推荐
  getHotarticle(){
    var key = app.getKey();
    var that = this;
    app.ajax('/Home/Index/hotarticle', "POST", { key: key }, function (res) {
      that.setData({
        hotarticle: res.data.data,
        successNum: that.data.successNum + 1
      })
      that.isLoaded()
    })
  },
  //文章详情
  toDetail(event){
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?id='+id,
    })
  },
  isLoaded(){
    let that = this;
    if(that.data.successNum==6){
      wx.hideLoading()
      that.setData({
        allLoaded:true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    this.getPartnerLst();
    this.getTelPhone();
    this.getAddress();
    this.getCategory();
    this.getBanner();
    this.getHotarticle()
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})